package com.py.payment.tools;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ToolsUtils {

    public final static String EXCEL2003 = "xls";
    public final static String EXCEL2007 = "xlsx";

    private final static String PATTERN = "\\s*|\t|\r|\n";

    public static boolean isNotEmpty(Object o) {
        return !isEmpty(o);
    }

    /**
     * 对象是否为空
     *
     * @author
     * @Date 2018/3/18 21:57
     */
    public static boolean isEmpty(Object o) {
        if (o == null) {
            return true;
        }
        if (o instanceof String) {
            if ("".equals(o.toString().trim())) {
                return true;
            }
        } else if (o instanceof List) {
            if (((List) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Map) {
            if (((Map) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Set) {
            if (((Set) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Object[]) {
            if (((Object[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof int[]) {
            if (((int[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof long[]) {
            if (((long[]) o).length == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 检查类型是否为Long
     * @param num
     * @return
     */
    public static boolean isLong(String num){
        try{
            Long retNums = Long.parseLong(num);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    public static String replaceBlank(String str){
        String dest = null;
        if(str == null){
            return dest;
        }else{
            Pattern p = Pattern.compile(PATTERN);
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
            return dest;
        }
    }

    /**
     * 替换中间字符
     * @param param
     * @param str
     * @return
     */
    public static String hideChar(String param,String str){
        StringBuffer sb = new StringBuffer();
        for(int i = 0;i<param.length(); i++){
            if(i == 0){
                sb.append(param.charAt(i));
            }else if(i == 1){
                sb.append(param.charAt(i));
            }else if(i == param.length()-9){
                sb.append(param.charAt(i));
            }else if(i == param.length()-3){
                sb.append(param.charAt(i));
            }else if(i == param.length()-2){
                sb.append(param.charAt(i));
            }else if(i == param.length()-1){
                sb.append(param.charAt(i));
            }else{
                sb.append(str);
            }
        }
        return sb.toString();
    }

    public static String addZeroForNum(String str, int strLength,int type) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                if(type==1){
                    //前面加
                    sb.append("0").append(str);
                }else{
                    //后面加
                    sb.append(str).append("0");
                }
                str = sb.toString();
                strLen = str.length();
            }
        }
        return str;
    }
}
